package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.PhoneArea;
import com.sls.web.manager.recharge.querybean.PhoneAreaQuery;
import com.sls.web.manager.recharge.service.PhoneAreaService;
import com.sls.web.manager.socket.SuperService;

/**
 * @author sxf
 * 
 */
@Service("phoneAreaService")
public class PhoneAreaServiceImpl extends SuperService implements
		PhoneAreaService {
	private static Logger log = Logger.getLogger(PhoneAreaServiceImpl.class);

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	public boolean insert(PhoneArea info, UserInfoVO userInfoVO)
			throws Exception {
		if (info == null || info.getPhone() == null) {
			return false;
		}
		if (info != null) {
			info.setPhone(getPhoneTo7(info.getPhone()));
		}
		this.callService(Constants.INSERT_PHONEAREA, null, info, userInfoVO);
		return true;
	}

	@Override
	public List<PhoneArea> queryList(PhoneAreaQuery query, UserInfoVO userInfoVO)
			throws Exception {
		if (query != null) {
			query.setPhone(getPhoneTo7(query.getPhone()));
		}
		List<PhoneArea> infoPage = callService(Constants.QUERY_PHONEAREA_LIST,
				null, query, userInfoVO, new TypeToken<List<PhoneArea>>() {
				}.getType());
		return infoPage;
	}

	public Pagination<PhoneArea> queryPage(PhoneAreaQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter PHONEAREAService queryPage method...");
		if (query != null) {
			query.setPhone(getPhoneTo7(query.getPhone()));
		}
		Pagination<PhoneArea> infoPage = callService(
				Constants.QUERY_PHONEAREA_PAGE, null, query, userInfoVO,
				new TypeToken<Pagination<PhoneArea>>() {
				}.getType());
		log.info("Leave PHONEAREAService queryPage method...");
		return infoPage;
	}

	public PhoneArea queryOne(PhoneAreaQuery query, UserInfoVO userInfoVO)
			throws Exception {
		log.info("Enter PHONEAREAService queryPage method...");
		if (query != null) {
			query.setPhone(getPhoneTo7(query.getPhone()));
		}
		PhoneArea infoPage = callService(Constants.QUERY_PHONEAREA_ONE, null,
				query, userInfoVO, PhoneArea.class);
		log.info("Leave PHONEAREAService queryPage method...");
		return infoPage;
	}

	public boolean update(PhoneArea info, UserInfoVO userInfoVO)
			throws Exception {
		if (info != null) {
			info.setPhone(getPhoneTo7(info.getPhone()));
		}
		this.callService(Constants.UPDATE_PHONEAREA, null, info, userInfoVO);
		return true;
	}

	public boolean delete(PhoneArea info, UserInfoVO userInfoVO)
			throws Exception {
		if (info != null) {
			info.setPhone(getPhoneTo7(info.getPhone()));
		}
		this.callService(Constants.DELETE_PHONEAREA, null, info, userInfoVO);
		return true;
	}

	/**
	 * 截取号码前7位
	 * 
	 * @param phone
	 * @return
	 */
	private String getPhoneTo7(String phone) {
		if (phone != null && phone.length() >= 7) {
			return phone.substring(0, 7);
		}
		return phone;
	}

}
